package com.zksk.admin.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zksk.admin.domain.orders.dto.OrdersDetailDto;
import com.zksk.admin.domain.orders.dto.OrdersDto;
import com.zksk.admin.domain.orders.vo.OrdersRequestVo;
import com.zksk.admin.service.orders.OrdersQuestionService;
import com.zksk.admin.service.orders.OrdersService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.Orders;
import com.zksk.mbg.admin.domain.OrdersQuestion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单类 信息操作处理
 *
 * @author zksk
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(value = "OrderController", tags = "订单相关接口")
public class OrderController extends BaseController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersQuestionService ordersQuestionService;

    /**
     * 订单列表展示
     * @param requestVo
     * @return
     */
    @Log(title = "订单管理-列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "获取订单列表")
    @PostMapping("/list")
    public R<IPage<OrdersDto>> list(@RequestBody OrdersRequestVo requestVo){
        IPage<OrdersDto> resultPage = ordersService.ordersList(requestVo);
        return R.ok(resultPage, "获取订单列表数据成功");
    }

    /**
     * 查看订单
     * @param ordersId
     * @return
     */
    @Log(title = "订单管理", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查看订单")
    @GetMapping("/{ordersId}")
    public R<Orders> select(@PathVariable("ordersId") Long ordersId){
        Orders byId = ordersService.getById(ordersId);
        return R.ok(byId, "查看订单数据成功");
    }

    /**
     * 查看订单详情 - 订单关联问题
     * @param ordersId
     * @return
     */
    @Log(title = "订单管理", businessType = BusinessType.SELECT)
    @ApiOperation(value = "查看订单详情-订单关联问题")
    @GetMapping("/detail/{ordersId}")
    public R<OrdersDetailDto> detail(@PathVariable("ordersId") Long ordersId){
        OrdersDetailDto dto = new OrdersDetailDto();
        Orders byId = ordersService.getById(ordersId);
        List<OrdersQuestion> list = ordersQuestionService.list(
                new LambdaQueryWrapper<OrdersQuestion>()
                        .eq(OrdersQuestion::getOrdersId, ordersId)
        );
        dto.setOrders(byId);
        dto.setQuestions(list);
        return R.ok(dto, "查看订单详情数据成功");
    }

    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除订单(逻辑删除)")
    public R<String> remove(@PathVariable List<Long> ids) {
        ordersService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }

}
